package com.imast.android.bys.ui.fragment;

import com.imast.android.bys.R;
import com.imast.android.bys.io.model.Producto;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

public class ProductoDialogFragment extends DialogFragment {
	
	private Producto producto;
	
	private EditText edtCantidad;
	

	public interface ProductoDialogListener { 
        public void onDialogPositiveClick(DialogFragment dialog, Producto producto, int cantidad); 
        public void onDialogNegativeClick(DialogFragment dialog); 
    } 
	
	ProductoDialogListener mListener;
	
	public ProductoDialogFragment(Producto producto) {
		this.producto = producto;
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		AlertDialog.Builder builder = new Builder(getActivity());
		
		LayoutInflater inflater = getActivity().getLayoutInflater();
		
		View view = inflater.inflate(R.layout.dialog_producto, null);
		edtCantidad = (EditText) view.findViewById(R.id.edtCantidad);
		
		builder.setTitle(producto.getTitulo());
		builder.setView(view)
			.setPositiveButton(R.string.dialogAceptar, new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					
					int cantidad = Integer.valueOf(edtCantidad.getText().toString());
					
					mListener.onDialogPositiveClick(ProductoDialogFragment.this, producto, cantidad);
				}
			})
			.setNegativeButton(R.string.dialogCancelar, new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					mListener.onDialogNegativeClick(ProductoDialogFragment.this);
				}
			});
		
		return builder.create();
	}
	
	 public void onAttach(Activity activity) {
	        super.onAttach(activity);
	        // Verify that the host activity implements the callback interface 
	        try { 
	            // Instantiate the NoticeDialogListener so we can send events to the host 
	            mListener = (ProductoDialogListener) activity;
	        } catch (ClassCastException e) { 
	            // The activity doesn't implement the interface, throw exception 
	            throw new ClassCastException(activity.toString()
	                    + " must implement NoticeDialogListener"); 
	        } 
	    }

}
